<template>
  <div class="container">
    <div class="cheader2">
      <van-icon
        @click="goBack"
        class="chl"
        name="arrow-left"
        color="#000"
        size="20"
      />
      <div class="chtitle">修改邮箱</div>
      <div class="djbox"></div>
    </div>
    <div class="ibox2">
      <div class="ili">
        <div class="ili-1">新邮箱</div>
        <div class="ili-2">
          <van-field v-model="submitForm.newEmail" placeholder="请输入邮箱" />
        </div>
      </div>
      <div class="ili">
        <div class="ili-1">新邮箱验证码</div>
        <div class="ili-2">
          <van-field
            v-model="submitForm.verificationCode"
            placeholder="请输入验证码"
            :border="false"
          />
          <span class="fasong" @click="sendEmailCode">{{ emailText }}</span>
        </div>
      </div>
    </div>
    <div class="btn" @click="handleSubmit">确定</div>
  </div>
</template>

<script setup lang='ts'>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { isEmail } from "@/utils/common";
import http from "@/utils/http";
import { Toast } from "vant";
import { useStore } from "@/store";
const store = useStore();
const router = useRouter();
const emailText = ref<number | string>("发送");
const submitForm = reactive({
  newEmail: "",
  verificationCode: "",
});

const sendEmailCode = () => {
  if (!isEmail(submitForm.newEmail)) {
    Toast("请输入正确的邮箱");
    return;
  }
  if (emailText.value === "发送") {
    emailText.value = 60;
    const timer = setInterval(() => {
      if (emailText.value <= 0) {
        emailText.value = "发送";
        clearInterval(timer);
        return;
      }
      (emailText.value as number) -= 1;
    }, 1000);
    store
      .dispatch("users/sendEmailCode", {
        email: submitForm.newEmail,
        type: 1,
      })
      .then((res: any) => {
        Toast(res.msg || res.data);
      });
  }
};
const handleSubmit = () => {
  if (!isEmail(submitForm.newEmail)) {
    Toast("请输入正确的邮箱");
    return;
  }
  if (!submitForm.verificationCode) {
    Toast("请输入验证码");
    return;
  }
  http.put("user/user/email", submitForm).then((res: any) => {
    if (res.code === 0) {
      Toast("修改邮箱成功");
      store.dispatch("users/updateUserInfo");
      setTimeout(() => {
        goBack();
      }, 1000);
    }
  });
};
const goBack = () => {
  router.go(-1);
};
</script>

<style lang="scss" scoped>
.cheader2 {
  z-index: 1001;
  width: 100%;
  height: 45px;
  background-color: #fff;
  font-size: 16px;
  color: #000;
  position: fixed;
  top: 0;
  left: 0;
  .chl {
    position: absolute;
    left: 16px;
    top: 50%;
    text-align: center;
    transform: translateY(-50%);
  }
  .chtitle {
    position: absolute;
    left: 50%;
    top: 50%;
    text-align: center;
    transform: translate(-50%, -50%);
  }
  .djbox {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    img {
      width: 16px;
      margin-right: 4px;
    }
    div {
      font-size: 13px;
      color: #878b97;
    }
  }
}
.ibox2 {
  padding-top: 70px;
  margin: 0px 20px;
  .ili {
    display: flex;
    flex-direction: column;
    margin-bottom: 14px;
    .ili-1 {
      font-size: 12px;
      color: #000;
      margin-bottom: 10px;
    }
    .ili-2 {
      background-color: #f1f1f1;
      border-radius: 4px;
      height: 38px;
      position: relative;
      .fasong {
        font-size: 13px;
        color: #36b4ea;
        position: absolute;
        right: 16px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 999;
      }
    }
  }
}
.btn {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #fff;
  background: #36b4ea;
  border-radius: 3px;
  width: calc(100% - 40px);
  margin:40px auto;
}
</style>